package com.ljf.tijian.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.ljf.tijian.dto.ApiResponse;
import com.ljf.tijian.dto.OrdersRequestDto;
import com.ljf.tijian.dto.OrdersResponseDto;
import com.ljf.tijian.dto.OrdersSearchDto;
import com.ljf.tijian.mapper.OrdersMapper;
import com.ljf.tijian.pojo.Orders;
import com.ljf.tijian.service.Impl.OrdersServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class OrdersService implements OrdersServiceImpl {

    @Autowired
    OrdersMapper ordersMapper;

    @Override
    public ApiResponse queryOrdersByManyCondition(OrdersRequestDto ordersRequestDto) {
//        System.out.println(ordersRequestDto.getPageNum());
        //***
        PageHelper.startPage(ordersRequestDto.getPageNum(),ordersRequestDto.getMaxPageNum());  //

        List<OrdersSearchDto> orderDtoList=ordersMapper.queryOrdersByManyCondition(ordersRequestDto);

        PageInfo<OrdersSearchDto> pageInfo=new PageInfo<>(orderDtoList,5);
        System.out.println("一共有多少页："+pageInfo.getPages());
        System.out.println("一共有多少条数据："+pageInfo.getSize());
        System.out.println("有多少条数据："+pageInfo.getList().size());



        //ApiResponse apiResponse=new ApiResponse(ApiResponse.CODE,ApiResponse.SUCCESS,orderDtoList);
        OrdersResponseDto ordersResponseDto=new OrdersResponseDto();
        ordersResponseDto.setList(orderDtoList);
        ordersResponseDto.setMaxPageNum(ordersRequestDto.getMaxPageNum());
        ordersResponseDto.setPageNum(ordersRequestDto.getPageNum());
        ordersResponseDto.setTotalRow((int)(pageInfo.getTotal()));



        ApiResponse apiResponse=new ApiResponse(200,"SUCCESS",ordersResponseDto);

        return apiResponse;
    }

    @Override
    public ApiResponse updateOrdersState(Orders orders){
        System.out.println(orders.getState());
        System.out.println(orders.getOrderId());
        ordersMapper.updateByPrimaryKeySelective(orders);
        return new ApiResponse(200,"SUCCESS");
    }
}
